package dyyx;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.URLDecoder;
import java.util.Date;

public class RequestHandleThread extends Thread {
	private File _rootDir;

	private Socket _socket;

	public RequestHandleThread(Socket paramSocket, File paramFile) {
		this._socket = paramSocket;
		this._rootDir = paramFile;
	}

	private static void sendHeader(BufferedOutputStream paramBufferedOutputStream, int paramInt, String paramString,
			long paramLong1, long paramLong2) throws IOException {
		paramBufferedOutputStream.write(("HTTP/1.0 " + paramInt + " OK\r\n" + "Date: " + (new Date()).toString()
				+ "\r\n" + "Server: MiniHttpServer/1.0\r\n" + "Content-Type: " + paramString + "\r\n"
				
				+ ((paramLong1 != -1L) ? ("Content-Length: " + paramLong1 + "\r\n") : "") + "Last-modified: "
				+ (new Date(paramLong2)).toString() + "\r\n" + "\r\n").getBytes());
	}

	private static void sendError(BufferedOutputStream paramBufferedOutputStream, int paramInt, String paramString)
			throws IOException {
		paramString = paramString + "<hr>" + "MiniHttpServer  http://codefun007.xyz/";
		sendHeader(paramBufferedOutputStream, paramInt, "text/html", paramString.length(), System.currentTimeMillis());
		paramBufferedOutputStream.write(paramString.getBytes());
		paramBufferedOutputStream.flush();
		paramBufferedOutputStream.close();
	}

	public void run() {
		BufferedInputStream bufferedInputStream = null;
		try {
			this._socket.setSoTimeout(30000);
			BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(this._socket.getInputStream()));
			BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(this._socket.getOutputStream());
			String str1 = bufferedReader.readLine();
			if (str1 == null || !str1.startsWith("GET ")
					|| (!str1.endsWith(" HTTP/1.0") && !str1.endsWith("HTTP/1.1"))) {
				sendError(bufferedOutputStream, 500, "Invalid Method.");
				return;
			}
			String str2 = str1.substring(4, str1.length() - 9);
			File file = (new File(this._rootDir, URLDecoder.decode(str2, "UTF-8"))).getCanonicalFile();
			if (file.isDirectory()) {
				File file1 = new File(file, "index.html");
				if (file1.exists() && !file1.isDirectory())
					file = file1;
			}
			if (!file.toString().startsWith(this._rootDir.toString())) {
				sendError(bufferedOutputStream, 403, "Permission Denied.");
			} else if (!file.exists()) {
				sendError(bufferedOutputStream, 404, "File Not Found.");
			} else if (file.isDirectory()) {
				if (!str2.endsWith("/"))
					str2 = str2 + "/";
				File[] arrayOfFile = file.listFiles();
				sendHeader(bufferedOutputStream, 200, "text/html", -1L, System.currentTimeMillis());
				String str = "Index of " + str2;
				bufferedOutputStream.write(
						("<html><head><title>" + str + "</title></head><body><h3>Index of " + str2 + "</h3><p>\n")
								.getBytes());
				for (byte b = 0; b < arrayOfFile.length; b++) {
					file = arrayOfFile[b];
					String str3 = file.getName();
					String str4 = "";
					if (file.isDirectory())
						str4 = "&lt;DIR&gt;";
					bufferedOutputStream
							.write(("<a href=\"" + str2 + str3 + "\">" + str3 + "</a> " + str4 + "<br>\n").getBytes());
				}
				bufferedOutputStream
						.write("</p><hr><p>MiniHttpServer  http://codefun007.xyz/ </p></body><html>".getBytes());
			} else {
				bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
				String str = (String) MiniHttpServer.MIME_TYPES.get(MiniHttpServer.getExtension(file));
				if (str == null)
					str = "application/octet-stream";
				sendHeader(bufferedOutputStream, 200, str, file.length(), file.lastModified());
				byte[] arrayOfByte = new byte[4096];
				int i;
				while ((i = bufferedInputStream.read(arrayOfByte)) != -1)
					bufferedOutputStream.write(arrayOfByte, 0, i);
				bufferedInputStream.close();
			}
			bufferedOutputStream.flush();
			bufferedOutputStream.close();
		} catch (IOException iOException) {
			if (bufferedInputStream != null)
				try {
					bufferedInputStream.close();
				} catch (Exception exception) {
				}
		}
	}
}
